export function getUrlCode() {
	// 截取url中的code方法
	var url = location.search;
	var theRequest = new Object();
	if (url.indexOf('?') != -1) {
		var str = url.substr(1);
		var strs = str.split('&');
		for (var i = 0; i < strs.length; i++) {
			theRequest[strs[i].split('=')[0]] = strs[i].split('=')[1];
		}
	}
	return theRequest.code || false;
}

function getCode() {
	// 非静默授权，第一次有弹框
	this.code = '';
	var local = window.location.href; // 获取页面url
	var appid = '';
	let id = 1
	var url = encodeURIComponent(local);
	this.code = getUrlCode(); // 截取codeurlEncode
	if (JSON.stringify(this.code) == '{}') {
		// 如果没有code，则去请求
		window.location.href =
			'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
			appid +
			'&redirect_uri=' +
			url +
			'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
	} else {
		let that = this
		uni.request({
			url: '/wx/userInfo/getUserInfo',
			data: {
				code: that.code.code
			},
			success(res) {
				console.log(res)
				that.userInfo = res.data.object.wxUser
				login(res.data.object.wxUser)
			},
			fail(err) {
				console.log(err)
			}
		})
	}
}

function login(userInfo) {
	let that = this
	// uni.request({
	// 	url: '/login',
	// 	method: 'POST',
	// 	data: {
	// 		username: userInfo.openid,
	// 		password: userInfo.openid
	// 	},
	// 	header: {
	// 		"content-type": "application/x-www-form-urlencoded"
	// 	},
	// 	success: res => {
	// 		console.log(res)
	// 		getApp().globalData.token = res.data.token
	// 		getApp().globalData.userInfo = res.data.user
	// 	},
	// 	fail: (err) => {
	// 		console.log(err)
	// 	},
	// });
}